﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using cognysClient.CognyService;
using System.Web.Security;

namespace cognysClient.Admin
{
    public partial class WebForm3 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        //Method for the search button, fills the listbox
        protected void btnSearchUser_Click(object sender, EventArgs e)
        {
            fillListBox();
        }

        //Method that fills all the textboxes with the selected persons information
        protected void lbxSearchUser_SelectedIndexChanged(object sender, EventArgs e)
        {
            Guid id = Guid.Parse(lbxSearchUser.SelectedItem.Value);

            var client = new CognyService.Service1Client();
            var person = client.getPerson(id);
            tbFirstNameEditUser.Text = person.Firstname;
            tbLastNameEditUser.Text = person.Lastname;
            tbPersonalEditUser.Text = person.SocialNumber;
            string firstName;
            string lastName;
            if (person.Firstname.Length < 3)
            {
                firstName = person.Firstname.Substring(0, 2) + person.Firstname[1];
            }
            else
            {
                firstName = person.Firstname.Substring(0, 3);
            }
            if (person.Lastname.Length < 3)
            {
                lastName = person.Lastname.Substring(0, 2) + person.Lastname[1];
            }
            else
            {
                lastName = person.Lastname.Substring(0, 3);
            }
            var personal = person.SocialNumber.Substring(0, 2);
            var Username = firstName.ToLower() + lastName.ToLower() + personal + "0";
            tbUserNameEditUser.Text = Username;
            MembershipUser u = Membership.GetUser(Username);
            string password;

            if (u != null)
            {
                    password = u.GetPassword();
                    tbPasswordEditUser.Text = password;
                    tbChangePasswordEditUser.Text = password;
                    
            }
            else
            {
                Response.Write("<script>alert('Kunde inte hämta lösenordet');</script>");
                tbPasswordEditUser.Text = "";
            }
        }

        //Method that fills the ListBox
        public void fillListBox()
        {
            if (IsPostBack)
            {
                string firstname = tbSearchFirstName.Text;
                string lastname = tbSearchLastName.Text;
                string personal = tbSearchPersonal.Text;

                var client = new CognyService.Service1Client();
                var results = (from x in client.getPersonsByNameAndPersonal(firstname, lastname, personal)
                               select new
                               {
                                   FullName = x.Firstname + " " + x.Lastname,
                                   ID = x.ID

                               }).ToList();

                lbxSearchUser.Items.Clear();
                lbxSearchUser.DataSource = results;
                lbxSearchUser.DataTextField = "FullName";
                lbxSearchUser.DataValueField = "ID";
                lbxSearchUser.DataBind();
            }
        }

        //Method that clears all the textboxes
        public void clearTextboxes()
        {
            tbFirstNameEditUser.Text = "";
            tbLastNameEditUser.Text = "";
            tbPersonalEditUser.Text = "";
            tbUserNameEditUser.Text = "";
            tbPasswordEditUser.Text = "";
            tbChangePasswordEditUser.Text = "";
        }


        //Method for the edit button
        protected void btnEditUser_Click(object sender, EventArgs e)
        {
            try
            {

                Person person = new Person();
                person.ID = Guid.Parse(lbxSearchUser.SelectedItem.Value);
                person.Firstname = tbFirstNameEditUser.Text;
                person.Lastname = tbLastNameEditUser.Text;
                person.SocialNumber = tbPersonalEditUser.Text;

                using (var client = new Service1Client())
                {
                    client.editPerson(person);
                }

                var firstName = person.Firstname.Substring(0, 3);
                var lastName = person.Lastname.Substring(0, 3);
                var personal = person.SocialNumber.Substring(0, 2);
                var Username = firstName.ToLower() + lastName.ToLower() + personal + "0";
                var personID = Guid.Parse(lbxSearchUser.SelectedItem.Value);

            

                MembershipUser u = Membership.GetUser(Username);

                if (string.IsNullOrEmpty(tbChangePasswordEditUser.Text))
                {
                }
                else
                {
                    u.ChangePassword(tbPasswordEditUser.Text, tbChangePasswordEditUser.Text);
                }


                Response.Write("<script>alert('Uppdatering lyckad!');</script>");
                clearTextboxes();
                fillListBox();
        }
        catch
        {
                Response.Write("<script>alert('Uppdatering misslyckad');</script>");
        }
        }

    }
}